<script setup>
import {ref, watch} from "vue";
import VIcon from "@/components/icon/VIcon.vue";

const props = defineProps({
  article: {
    type: Object,
    default: () => {
      return {}
    }
  }
});

const emits = defineEmits(['lookup']);
const articleInfo = ref({});

watch(() => props.article, (newVal, oldVal) => {
  // console.log(newVal, oldVal)
  articleInfo.value = newVal;
}, {deep: true, immediate: true});

const postOtherInfo = ref([
  {icon: 'Calendar', value: 'publishedTime', label: '发布日期'},
  {icon: 'CollectionTag', value: 'categoryName', label: '文章分类'},
  {icon: 'View', value: 'views', label: '浏览量'},
  {icon: 'Star', value: 'likes', label: '点赞数'},
]);

const handleClick = () => {
  emits('lookup', articleInfo.value);
};
</script>

<template>
  <div class="v-post-container" @click="handleClick">
    <div class="v-post-image">
      <el-image :src="articleInfo?.coverImage" fit="fill">
        <template #error>
          <v-icon icon="Picture" size="80"/>
        </template>
      </el-image>
    </div>
    <div class="v-post-content">
      <div class="v-post-title">{{ articleInfo?.title }}</div>
      <div class="v-post-brief">{{ articleInfo?.brief }}</div>
      <div class="v-post-info">
        <template v-for="item in postOtherInfo">
          <div>
            <el-tooltip effect="dark" :content="item?.label" placement="top">
              <v-icon :icon="item?.icon" size="16"/>
            </el-tooltip>
            <span>{{ articleInfo[item?.value] }}</span>
          </div>
        </template>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.v-post-container {
  background-color: #ffffff;
  display: flex;
  justify-items: center;
  padding: 10px 20px;
  border-radius: 4px;
  box-shadow: 0 3px 8px 6px rgba(7, 17, 27, 0.05);
  transition: all .3s;
  margin-bottom: 10px;
}

.v-post-container:hover {
  cursor: pointer;
  box-shadow: 0 3px 8px 6px rgba(7, 17, 27, 0.09);
}

.v-post-image {
  width: var(--post-image-size);
  height: var(--post-image-size);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all .5s;
  
  img {
    width: 100%;
    height: 100%;
  }
}

.v-post-container:hover > .v-post-image {
  transform: scale(1.1);
}

.v-post-content {
  width: calc(100% - var(--post-image-size) - 10px);
  //background-color: #cccccc;
  margin-left: 10px;
  
  .v-post-title {
    width: 100%;
    padding: 6px;
    box-sizing: border-box;
    font-size: 20px;
    font-weight: 700;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .v-post-brief {
    font-size: 14px;
    color: #999999;
    display: flex;
    flex-direction: row;
    padding: 0 6px;
    box-sizing: border-box;
    height: 120px;
    overflow: hidden;
  }
  
  .v-post-info {
    font-size: 12px;
    margin-top: 5px;
    padding: 0 6px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    
    > div {
      display: flex;
      align-items: center;
      margin-right: 10px;
      
      > span {
        color: #999999;
      }
    }
  }
}
</style>